<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <SmForm
      class="-mb-15px"
      ref="formRef"
      isCol
      label-width="150px"
      v-model="formData"
      v-loading="formLoading"
      :options="requestFormOptions"
      :getModelValue="getFormData"
    >
      <!-- <template #roleItems>角色权限进行插槽展示</template> -->
    </SmForm>

    <template #footer>
      <el-button @click="submitFormDB" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { addProperty } from "@/components/SmForm/src/utils";
import { createDBFn } from "@/utils/decorate";
import {
  getCustomerList,
  getVehicleCategoryList,
  getVehicleInsuranceItemsList,
  getVehicleNaturesList,
} from "@/commonData";
import { createInputList } from "@/utils/form";
import { invoiceCategoryList, invoiceTypeList } from "@/commonData/dict";
import { numToString } from "@/utils/transform";
import InsuranceRateSchedulesAPI from "@/api/config/insurance-rate-schedules";

/** 仓库 表单 */
defineOptions({ name: "OpenForm" });

const { t } = useI18n(); // 国际化

const dialogVisible = ref(false); // 弹窗的是否展示
const dialogTitle = ref(""); // 弹窗的标题
const formLoading = ref(false); // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref(""); // 表单的类型：create - 新增；update - 修改
const initFormData = () => {
  return {};
};
const formData = ref(initFormData());
const formRef = ref(); // 表单 Ref

const vehicleInsuranceItemsList = ref([]);
const vehicleNaturesList = ref([]);
const vehicleCategoryList = ref([]);

const requestFormOptions: any = ref([]);

const createRequestFormOptions = () => {
  const list = createInputList([
    { type: "input-number", prop: "ageLowerLimit", label: "年限下限值" },
    { type: "input-number", prop: "ageUpperLimit", label: "年限上限值" },
    { type: "input-number", prop: "basePremium", label: "保费" },
    { type: "input-number", prop: "coverageLimit", label: "保额" },

    { type: "input-number", prop: "monthlyDepreciationRate", label: "月折旧系数" },

    { type: "input-number", prop: "rateValue", label: "费率" },
    { type: "input-number", prop: "seatWeightLowerLimit", label: "座位或重量下限值" },
    { type: "input-number", prop: "seatWeightUpperLimit", label: "座位或重量上限值" },

    // vehicleCategoryId-车辆种类ID
    // vehicleInsuranceItemId-统筹项目ID-必填
    // vehicleNatureId-车辆性质ID-必填

    { prop: "calculationFactors", label: "算费因素" },

    {
      type: "select",
      data: vehicleCategoryList,
      prop: "vehicleCategoryId",
      label: "车辆种类",
    },
    {
      prop: "vehicleNatureId",
      type: "select",
      data: vehicleNaturesList,
      label: "车辆性质",
      attrs: { requiredFlag: true },
    },

    {
      type: "select",
      data: vehicleInsuranceItemsList,
      prop: "vehicleInsuranceItemId",
      label: "统筹项目",
      attrs: { requiredFlag: true },
    },
  ]);

  addProperty(list);
  return list;
};

// const createRequestFormOptions = () => {
//   return mockForm(mockArr);
// };

requestFormOptions.value = createRequestFormOptions();

/** 打开弹窗 */
const open = async (type: string, id?: string) => {
  dialogVisible.value = true;
  dialogTitle.value = t("action." + type);
  formType.value = type;
  resetForm();

  getVehicleInsuranceItemsList(vehicleInsuranceItemsList);
  getVehicleNaturesList(vehicleNaturesList);
  getVehicleCategoryList(vehicleCategoryList);

  // 修改时，设置数据
  if (id) {
    formLoading.value = true;
    try {
      formData.value = await InsuranceRateSchedulesAPI.getInfo(id);

      // 主动触发表单数据回显
      formRef.value.initForm();
    } finally {
      formLoading.value = false;
    }
  }
};
defineExpose({ open }); // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(["success"]); // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate();
  // 提交请求
  formLoading.value = true;
  try {
    const data = formData.value as unknown as any;
    if (formType.value === "create") {
      await InsuranceRateSchedulesAPI.create(data);
    } else if (formType.value === "update") {
      await InsuranceRateSchedulesAPI.update(data.id, data);
    }
    ElMessage.success("操作成功");
    dialogVisible.value = false;
    // 发送操作成功的事件
    emit("success");
  } finally {
    formLoading.value = false;
  }
};
const submitFormDB = createDBFn(submitForm);

/** 重置表单 */
const resetForm = () => {
  formData.value = initFormData();
  formRef.value?.resetFields();
};

const getFormData = () => {
  return formData.value;
};
</script>
